﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Diagnostics;
namespace RLZY
{
    public partial class FormGJJ_out_Add : Form
    {
        public FormGJJ_out_Add()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormGJJ_out_Add childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormGJJ_out_Add ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormGJJ_out_Add();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }

        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.myGZ mygz = new myGZ();
                权限 = model.备注;
                //this.txtBM.Text = 权限;
                BLL.GJJ_Out gjj = new BLL.GJJ_Out();
                DataSet ds = gjj.GetList(0, "", "id desc");
                //DataSet ds = mygz.GetListPart(0, 权限);
                this.BindData(ds);
               
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

     

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        

       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string strErr = "";
                if (this.txtGJJNumber.Text.Trim().Length == 0)
                {
                    strErr += "公积金号不能为空！\r\n";
                }
                if (this.txtMoney.Text.Trim().Length == 0)
                {
                    strErr += "取出公积金不能为空！\r\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                //BLL.userpassword bllpwd = new BLL.userpassword();
                //Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                //others.xlmoon xl = new xlmoon();

                //权限 = model.备注;

                //DataSet ds = xl.GetListLG(this.txtName.Text.Trim(), this.txtBZ.Text.Trim());
                //this.BindData(ds);
                this.ReadGJJ_RZ();
                this.Read_Last_GJJ();

                Model.GJJ_Out model =new  Model.GJJ_Out();
                BLL.GJJ_Out bll = new BLL.GJJ_Out();
                model = bll.GetModel(id);
                model.公积金号 = txtGJJNumber.Text.Trim();
                model.姓名 = txtName.Text.Trim();
                model.取出公积金 =Convert.ToDecimal( txtMoney.Text.Trim());
                model.日期 = dtpDate.Text;
                model.备注 = this.txtBZ.Text.Trim();
                if (bll.Update(model)) 
                {
                    DataSet ds = bll.GetList(0, "", "id desc");
                    this.BindData(ds);
                    this.txtBZ .Text = "";
                    this.txtGJJNumber.Text  = "";
                    this.txtMoney.Text  = "";
                    txtName.Text = "";


                }

            }
            catch (Exception ex) { LOG.Error(ex); }
           
        }

        private void Read_Last_GJJ()
        {
            others.GJJ gjj = new GJJ();
            DataSet ds = gjj.GetLast_GJJ(this.txtGJJNumber.Text.Trim());
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.LastGJJ = Convert.ToDecimal(ds.Tables[0].Rows[0]["公积金入帐"].ToString().Trim ());
                this.txtName.Text = ds.Tables[0].Rows[0]["姓名"].ToString();
            }
            else
            {
                this.LastGJJ = 0m;
                this.txtName.Text = "";
            }
        }

        private void ReadGJJ_RZ()
        {
            others.GJJ gjj=new GJJ ();

            DataSet ds = gjj.GetListSumGJJ(this.txtGJJNumber.Text.Trim());
            if (ds.Tables[0].Rows.Count > 0)
            { 
                this.VarGJJ = Convert.ToDecimal(ds.Tables[0].Rows[0]["公积金入帐"].ToString().Trim ()); 
            }
            else
            {
                this.VarGJJ = 0m;
            }
        }

        
        int id;

        private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            try
            {
                int i = this.ultraGrid1.ActiveRow.Index;
                id = Convert.ToInt32(this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim());
                this.txtGJJNumber.Text = this.ultraGrid1.Rows[i].Cells[2].Value.ToString().Trim();
                this.txtName.Text = this.ultraGrid1.Rows[i].Cells[1].Value.ToString().Trim();
                this.txtMoney.Text = this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
                this.txtBZ.Text = this.ultraGrid1.Rows[i].Cells[4].Value.ToString().Trim();
                this.dtpDate.Text = this.ultraGrid1.Rows[i].Cells[5].Value.ToString().Trim();

            }
            catch
            {
                //Common.RecordCommLog(ex);
                return;
            }

        }


        public decimal VarGJJ { get; set; }

        public decimal LastGJJ { get; set; }

        private void txtMoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13 && e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != 46) e.Handled = true;

            int a = txtMoney.Text.IndexOf('.');

            if (e.KeyChar == 46 && a > 0) e.Handled = true;
        }

        private void txtGJJNumber_Leave(object sender, EventArgs e)
        {
            this.Read_Last_GJJ();
        }
    }
}
